<?php
/**
 * 部门管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class SectorController extends BaseController {

	/**
	 * 获取部门信息
	 *
	 * @return array  后台员工自己部门以及下属部门
	 */
	public function index()
	{
        $Mdo = D("Admin/Sector");
        $MUser = D('Admin/User');
		$wheres = array();
		$wheres['is_deleted'] = array('eq', $Mdo::STATE_OK );
		$secs = $Mdo->field("sector_id,info,sector_name,parent_sector,manager_id")->where( $wheres )->select();
        $_secs = array();
        foreach( $secs as $sc ){
            $_secs[$sc['sector_id']] = $sc;
        }
        if( 1 == $this->m_user['user_id'] ){
            $arr = list_to_tree($secs,'sector_id','parent_sector','_child', 0 );
        }
        else{
            /**
             * BUG FIX:增加自己负责的部门
             * @var [type]
             */
            $arr = array();
            $arr[]=$_secs[$this->m_user['sector_id']];
		    $arr[0]['_child'] = list_to_tree($secs,'sector_id','parent_sector','_child', $this->m_user['sector_id']);
        }
		$re = array('rcode'=>1,'data'=>0,'msg'=>'ok');
        $re['users'] = $Mdo->alias("s")
                             ->field("u.user_id,u.sector_id,u.nickname,s.sector_name,s.info")
                             ->join("LEFT JOIN user u on s.manager_id=u.user_id")
                             ->where('state!=-1')
                             ->order(" sector_id ,CONVERT(u.nickname USING gbk) ASC")
                             ->select();
		$re['data'] = $arr;
        $re['sector'] = $_secs[$this->m_user['sector_id']];
		e_json( $re );
	}

    /**
     *  部门信息编辑
     *
     *
     * @return [type] [description]
     */
	public function edit()
    {
        $res = array('rcode'=>0,'data'=>NULL,'msg'=>'Server Busy!');
        $res['msg'] = L('SERVER_BUSY');

        if( IS_POST ){

            $sector_id = I('post.sector_id','','intval');
            $sector_name =  I('post.sector_name','','trim');
            $info = I('post.info','','trim');
            $parent_sector = I('post.parent_sector','','intval');
            $manager_id = I('post.manager_id');
            $Mdo = D("Sector");
            $r = $Mdo->edit_sector($sector_id, $sector_name, $parent_sector, $info, $manager_id );
            if( $r && $r['r'] ){
                $res['rcode'] = 1;
                $res['msg'] = 'OK';
            }
            else{
                $res['msg'] = $r['i'];
            }
        }


        e_json( $res );
    }

    public function add()
    {
        if( IS_POST ){
            $res = array('rcode'=>0,'data'=>NULL,'msg'=>'Server Busy!');
            $res['msg'] = L('SERVER_BUSY');
            $sector_name = I('post.sector_name','','trim');
            $info = I('post.info','','trim');
            $parent_sector = I('post.parent_sector');
            $manager_id = I('post.manager_id' );
            $Mdo = D("Sector");
            $r = $Mdo->add_sector( $sector_name, $parent_sector , $info, $manager_id );
            if( $r['r'] ){
                $res['rcode'] = 1;
                $res['data'] = $r['d'];
                $res['msg'] = 'OK';
            }else{
                $res['msg'] = $r['i'];
            }

            e_json( $res );
        }
    }

    public function delete()
    {
        $res = array('rcode'=>0,'data'=>NULL,'msg'=>'Server Busy!');
        $res['msg'] = L('SERVER_BUSY');
        $id = I('post.id','','intval');
        $Mdo = D('Sector');
        $r = $Mdo->remove_sector( $id );

        if( $r && $r['r']){
            $res['rcode'] = 1;
            $res['msg'] = 'OK';
        }
        else{
            $res['msg'] = $r['i'];
        }
        e_json( $res );
    }
}